/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.AdminMajor;

import com.ControlEscolar.Beans.Curriculum;
import com.ControlEscolar.Beans.Major;
import com.ControlEscolar.Exceptions.AlreadyExistExp;
import com.ControlEscolar.Services.CurriculumService;
import com.ControlEscolar.Services.MajorService;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

/**
 *
 * @author Kossel
 */
public class AdminMajor extends MappingDispatchAction {

MajorService majorService = new MajorService();
CurriculumService curriculumService = new CurriculumService();
ArrayList results=new ArrayList();

    public void add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

        MajorForm majorForm = (MajorForm) form;
          Major major = new Major();
          major.setID_Major(majorForm.getMajorKey());
          major.setMajorName(majorForm.getMajorName());
           try {
          majorService.createMajor(major);
          response.getWriter().print("success");
        }catch(AlreadyExistExp ex){
             response.getWriter().print("Ya existe una carrera con la clave");

        }

    }

     public ActionForward populate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
         results=majorService.getMajorList();
         HttpSession session = request.getSession();
         session.setAttribute("majorList", results);
    return mapping.findForward("success");

    }

    public void addCurriculum(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          CurriculumForm curriculumForm = (CurriculumForm) form;
          Curriculum curriculum = new Curriculum();
          curriculum.setCurriculumName(curriculumForm.getCurriculumName());
          curriculum.setID_Major(curriculumForm.getMajorID());
          curriculum.setTotalSemester(Integer.parseInt(curriculumForm.getNumSemester()));
        try{
          curriculumService.createCurriculum(curriculum);
          response.getWriter().print("success");
            }catch(AlreadyExistExp ex){
             response.getWriter().print("Ya existe un plan de estudio con la clave");

                }

    }

    public ActionForward getCurriculumList (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
         results=curriculumService.getCurriculumList();
         HttpSession session = request.getSession();
         session.setAttribute("curriculumList", results);
        return mapping.findForward("success");
    }

    public void getCurriculumSemester (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          String subjectKey=request.getParameter("id");
          int numSemester = curriculumService.getTotalNumOfSemesters(subjectKey);
          String json="{\"key\":"+numSemester+"}";
          response.getWriter().print(json);
    }


}
